﻿/*********************************************************************
 * CopyRight (c) 2009-2014 HangJing Teconology. All Rights Reserved.
 * FileName : 
 * Function : 
 * Created by jijunjian at 2015-08-17 16:19:29.
 * E-Mail   : jijunjian@ihangjing.com
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Hangjing.Model;
using Hangjing.SQLServerDAL;
using System.Reflection;
using System.Collections;
namespace Hangjing.WebCommon
{
    /// <summary>
    /// 配送员数据操作日志
    /// </summary>
    public class OperateLogManager<T> where T : new()
    {
        public string adminname;
        public string tableName;//表名
        /// <summary>
        /// 配送员实体字段名与含义
        /// </summary>
        Hashtable columnInfos;

        /// <summary>
        /// 日志管理构造函数
        /// </summary>
        public OperateLogManager(string _adminname,string _tableName)
        {
            adminname = _adminname;
            columnInfos = new Hashtable();
            tableName = _tableName;
            MethodInfo methodInfo = this.GetType().GetMethod("Init_"+tableName+"_ColumnInfos", BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
            methodInfo.Invoke(this, null);
        }

        /// <summary>
        /// 比较两个实体，然后返回实体中每个属性值不同的内容
        /// </summary>
        /// <param name="oldObj"></param>
        /// <param name="newObj"></param>
        /// <returns></returns>
        public IList<deliverlogInfo> Compare(T oldObj, T newObj,string delivername)
        {
            IList<deliverlogInfo> changeslog = new List<deliverlogInfo>();

            T obj = Activator.CreateInstance<T>();
            Type type = obj.GetType();
            PropertyInfo[] pis = type.GetProperties();

            for (int j = 0; j < pis.Length; j++)
            {
                object o = pis[j].GetValue(oldObj, null);
                object n = pis[j].GetValue(newObj, null);
                if (!IsEqual(pis[j].PropertyType, o, n) && columnInfos.ContainsKey(pis[j].Name))
                {
                    deliverlogInfo item = new deliverlogInfo();
                    item.admin = adminname;
                    item.optime = DateTime.Now;
                    item.eventname = "修改";
                    item.columntext = columnInfos[pis[j].Name].ToString();
                    item.columnname = pis[j].Name;
                    item.oldvalue = o.ToString();
                    item.newvalue = n.ToString();
                    item.reveint1 = 0;
                    item.reveint2 = 0;
                    item.revevar1 = delivername;
                    item.revevar2 = tableName;

                    changeslog.Add(item);

                }
            }

            return changeslog;
        }

        private bool IsEqual(Type dataType, object oldObj, object newObj)
        {
            if (oldObj == null && newObj == null)
                return true;

            if (dataType == typeof(int))
            {
                return (int)oldObj == (int)newObj;
            }
            else if (dataType == typeof(decimal))
            {
                return (decimal)oldObj == (decimal)newObj;
            }
            else if (dataType == typeof(double))
            {
                return (double)oldObj == (double)newObj;
            }
            else if (dataType == typeof(Guid))
            {
                return (Guid)oldObj == (Guid)newObj;
            }
            else if (dataType == typeof(DateTime))
            {
                return (DateTime)oldObj == (DateTime)newObj;
            }
            else
                return oldObj.Equals(newObj);

        }

        public void Init_Deliver_ColumnInfos()
        {
            columnInfos.Add("GpsIMEI", "群组");
            columnInfos.Add("Name", "姓名");
            columnInfos.Add("revefloat1", "保证金");
            columnInfos.Add("UserName", "用户名");
            columnInfos.Add("Password", "密码");
            columnInfos.Add("Phone", "电话");
            columnInfos.Add("Status", "状态");
            columnInfos.Add("workstate", "工作状态");
            columnInfos.Add("cartype", "车辆");
            columnInfos.Add("carower", "车辆所属");
            columnInfos.Add("carnum", "车牌");
            columnInfos.Add("post", "职位类别");
            columnInfos.Add("grade", "等级");
        }

        public void Init_Custorder_ColumnInfos()
        {
            columnInfos.Add("Mergeorderremark", "连单备注");
            columnInfos.Add("OrderAttach", "备注");
            columnInfos.Add("cardids", "其他备注");
            columnInfos.Add("OrderSums", "总金额");
            columnInfos.Add("cardpay", "配送距离");
            columnInfos.Add("shopdiscountmoney", "配送费");

            columnInfos.Add("delivergetfee_base", "配送员收入基数");
            columnInfos.Add("DeliverGetSendFee", "配送员收入");

            columnInfos.Add("Advance", "垫付现金");
            columnInfos.Add("Commission", "佣金");
        }

        /// <summary>
        /// 复制一个对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public T Clone(T obj)
        {
            Type objTye = typeof(T);
            T model = new T();

            PropertyInfo[] properties = objTye.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (!property.IsSpecialName)
                {
                    object o = property.GetValue(obj, null);
                    property.SetValue(model, o, null);
                }


            }
            return model;
        }
    }

    /// <summary>
    /// 表名，枚举
    /// </summary>
    public enum TableENUM : int
    {
        Deliver = 0,
        Custorder = 1
    }
}
